<?php

/**
 * @Author:      zh
 * @DateTime:    2017-09-11
 * @Description: 接收随行付推送数据
 */
class ReportSuiXingPayController extends BaseController
{
    /**
     * @Author:      zh
     * @DateTime:    2017-09-11
     * @Description: 接收数据
     */
    public function receiveDataFromSXP(){
        $response = array('return_code' => "SUCCESS", 'return_msg' => "处理成功",'tranSeqNo'=>'');
        do{
            $result1 = file_get_contents('php://input');
//            $result = "<string>BCEA02E1365771CFE70083C5DBE86B7565934642450C1F8AFA2F6F824E10C9295BAC4DC15F083D546535EA5C9EC1D122024E09F86461ACB42D3ADFFD6CAF255EC380AFDB2702E7D7A60A3AC53397FBA921811A1327A30AF05EF688AC107FAF2FBC6315A670FFE7A1744C2565D9FB0C853363777E46628F5701A85C251592B683031A94A4625352BD95CA057080E19FEECA23B84119FA90683674C9C89E9299DADDFD85C8B87CE263E0ADA7DE0CDDE64BE35B520D7D7CA1D7C16CF9D013F7B5AD2E7F4F8E9CD16EA063C6F58952BD93AD1F5D469EA68B1EE873708A4DE0F04EF92ADB8587DA3BAE1889EFB22E6E12F67611023D9CB27ABF71D894873D9DCCF98EE5904F2047DB0522AD04F037962CB8440A49BF14701B17D2532EB6F719566ACE7CAFCC8370CB0C2DAF952B9497525564BDC0A92DF06EEAE236EDBAC8E3289E17C7A733D277CE413A0AB27B993E207E69CC17D1FA0423F1629DDFFF802C4B843079249EAA75281CF25EAD2479F56C19F8</string>";
//            $result = "BCEA02E1365771CFE70083C5DBE86B7565934642450C1F8ABD45E2B8BD10DFE10D1290E6D98E4E05951B97DA703F2C57EEF63315B516A00729D54687472CF57B0BBCCDFA5223C140C4FB382368EB382BD45E022FB70862F4AB6188A88F485FDFADBD38BA9D6B75BD53D4073B83F14EB4FB43E2D159B79DA6FD870DBD7F01A870FA6BC949F8D99DB3E6120BF3DB7ABE888806DD6C5B47CB8D7704449F351F0118CFFBE622D215392A9C38A82EFCFE8F9E2DD88C1D220C69393EF4DA13A74A07A7C150504E74E749027F79243DBBA4BED52CAE5D29D220AC5B6AF02D414B334D0B6B9FF96F1297961B6B228457B8F9BA7334873879563B79111481E7493E4DE5E4369F3BB320F81332962F6678001DE43B79DD35D88539C3EB030365D78D9BF649419AEC4FCE9606FF5B7168961F60D82B33AA9BBD8D6CBDBFA3862B40E9846C52B357E94E49F43C0F1C9EEBD1B5B87B41F2F3A8FC4DA76AF574CE013D822AD29FA2C881E005E7C5F0BE386FD89430A41D";
            $result2 = Common::handStr($result1);
            Log::info(['posts'=>$result2]);
            $res_json = '';
            $res_arr = [];
//            $result = "19F13346FA75D3613D7295DBECBD166F434F3F5DA7E85B47561217167A561D97BCDB059761D3FF9F9D507BD9A9F728B3929D5A0E4D2E8B9460B1667313B711A18186CF66E2D6F739E6D148CB7E21D0DF6360BBCD4E1CB7752A5FBE737588185E96A0A9FBDD83B20EA12089D826C8CEDC92F6250D51823354196ABB322A2375293DCEED36E00E68D8C08AE8CCA122454429173A7063DC38825B92CBE0A3B657EBB5E5103AD883D820D34503A4C0DDAF0AA3804E3009F9D9E54330539E16AB9AB308709DD0B35D0C5E59EE8840134F8A5AB870BA14D24CFB1A2DDAB61EACABB1B58785D974ADE020CC4DA4B43FC87F4A183E803C4FD4B4EAAA6E0E7F9CDD2F6F67B0E047F1A9335275BA8CB80CA4D2A3C474200C33C874D32332C0886D44E8A9584105B41C71BD6986AC0DE49F032C1A7ED4A93F1AC90798B4B709806F4BD919788FC63760196E744616B2C4A9F53F03709E3E6AA3F5506E97DB8038A015139677";
            //解密
            try {
                $res_json = Common::MD5AndDES($result2);
            }catch(Exception $e){
                Log::debug($e);
                $response = array('return_code' => "FAIL", 'return_msg' => "解码失败",'tranSeqNo'=>'');
                break;
            }
            if($res_json && strlen($res_json) > 0) {
                $res_arr = json_decode($res_json,true);
                Log::debug(['解密后 Array'=>$res_arr]);
                if(!is_array($res_arr)){
                    $response = array('return_code' => "FAIL", 'return_msg' => "非数组",'tranSeqNo'=>'');
                    break;
                }
            }else{
                $response = array('return_code' => "FAIL", 'return_msg' => "转数组失败",'tranSeqNo'=>'');
                break;
            }
            if($res_arr) {
                $sign = $res_arr['sign'];
                $old_arr = $res_arr;
                //验证签名
                unset($res_arr['sign']);
                $res_arr_no_sign = $res_arr;
                $sig_str = Common::getStr($res_arr_no_sign);
                $verify_res = Common::verifySign2($sign,$sig_str);
                if($verify_res){
                    //保存
                    $old_arr['posts'] = $result2;
                    try {
                        $save_res = SXPPushRecord::saveData($old_arr);
                    }catch(Exception $e){
                        Log::error(['随行付数据保存失败,suixingpay_error'=>$e]);
                        $response = array('return_code' => "FAIL", 'return_msg' => "数据有误，保存失败",'tranSeqNo'=>'');
                        break;
                    }
                    if($save_res == 'ok') {
                        $response['tranSeqNo'] = $res_arr['tranSeqNo'];
                    }else{
                        $response = array('return_code' => "FAIL", 'return_msg' => "数据有误，保存失败",'tranSeqNo'=>'');
                    }
                    break;
                }else{
                    $response = array('return_code' => "FAIL", 'return_msg' => "验签失败",'tranSeqNo'=>'');
                    break;
                }
            }
        }while(false);
        Log::info("info", array("system" => "report" ,"event" => "receiveDataFromSXP", 'input' =>Input::all() ,"output" => $response));
        return Common::json_encode_ex($response);
    }
    /**
     * cms 导入旺 pos 数据时触发
     */
    //导入随行付记录
    public function putSXPRecords(){
        $date_start = Input::get('date_start');
        $date_end = Input::get('date_end');
        try {
            FinanceStatement::putSXPRecords($date_start, $date_end);
        }catch(Exception $e){
            $res = ['status'=>-1,'msg'=>'put_fail'];
            Log::info($res);
            return json_encode(['status'=>-1,'msg'=>'fail']);
        }
        $res = ['status'=>1,'msg'=>'put_success'];
        return json_encode($res);
    }
    //对账
    public function statement(){
        $date_start = Input::get('date_start');
        $date_end = Input::get('date_end') . " 23:59:59";
        $wirs = WposFinanceDetail::whereBetween('payDate',[$date_start,$date_end])->select('id')->get();
        try {
            foreach($wirs as $wi){
                FinanceStatement::statement(2,$wi->id);
            }
        }catch(Exception $e){
            $res = ['status'=>-1,'msg'=>'statement_fail'];
            return json_encode($res);
        }
        $res = ['status'=>1,'msg'=>'statement_success'];
        return json_encode($res);
    }
}